
<template>
  <div class="department-container">
    <div class="app-container">

      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="bank" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">一级部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree
          default-expand-all
          :data="data"
          :props="defaultProps"
        >
          <template slot-scope="{ data }">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width: 100%">
              <el-col :span="20">
                <svg-icon icon-class="bank" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">查看部门</el-dropdown-item>
                        <el-dropdown-item
                          v-if="!data.children.length"
                          @click.native="del(data.id)"
                        >删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>

    </div>
    <el-dialog
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :title="isEdit ? '编辑部门' : '添加部门' "
      :visible.sync="showDialog"
      @close="hDialogClose"
    >
      <dept-dialog
        ref="depDialog"
        :pid="arr"
        :edit="isEdit"
        :origin="originList"
        @close="hClose"
        @success="hSucces"
      />
    </el-dialog>
  </div>
</template>
<script>
import { getDepartments, delDepartment } from '@/api/department'
import { tranListToTreeData } from '@/utils'
import deptDialog from './deptDialog.vue'

export default {
  components: { deptDialog },
  data() {
    return {
      arr: '',
      showDialog: false, // 是否显示弹层
      isEdit: false, // 默认 是false
      data: [],
      defaultProps: {
        children: 'children',
        label: 'text'
      },
      originList: []// 校验的参考
    }
  },
  created() {
    // 发请求
    this.loadDepartment()
  },

  methods: {
    // 父接收子传值、
    hSucces() {
      // 关闭弹层
      this.showDialog = false
      // 更新数据
      this.loadDepartment()
    },
    // 用户点击了添加
    hAdd(id) {
      this.showDialog = true
      this.arr = id
    },
    // 用户点击了编辑
    hEdit(id) {
      // 1.更新当前的id
      this.arr = id
      // 2.设置状态为编辑
      this.isEdit = true
      // 3. 打开弹框
      this.showDialog = true
      // 父组件调用子组件
      this.$nextTick(() => {
        this.$refs.depDialog.loadDepartementDetail()
      })
    },
    // 用户点击了删除
    async  doDel(id) {
      await delDepartment(id)
      // 删除成功提示
      this.$message('删除成功')
      // 更新数据
      this.loadDepartment()
    },
    del(id) {
      // 1.弹框提示
      this.$confirm('你确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // await this.$store.dispatch('user/logout')
        // this.$router.push(`/login?return_url=` + encodeURIComponent(this.$route.fullPath))
        this.doDel(id)
      }).catch(() => {
        // 用户取消退出
      })
    },
    hDialogClose() {
      this.$refs.depDialog.resetFrom()
    },
    hClose() {
      this.showDialog = false
    },
    // 加载数据
    async loadDepartment() {
      const res = await getDepartments()
      console.log('res', res)
      this.originList = res.data.depts.map(item => {
        return {
          id: item.id,
          pid: item.pid,
          name: item.name,
          code: item.code

        }
      })
      // 删除第一个
      res.data.depts.shift()
      this.data = tranListToTreeData(res.data.depts)
    },
    handleNodeClick(data) {
      console.log(data)
    }
  }
}
</script>
